package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.SpuImage;
import com.atguigu.gmall.model.product.SpuInfo;
import com.atguigu.gmall.model.product.SpuSaleAttr;
import com.atguigu.gmall.model.product.SpuSaleAttrValue;
import com.atguigu.gmall.product.service.SpuImageService;
import com.atguigu.gmall.product.service.SpuInfoService;
import com.atguigu.gmall.product.service.SpuSaleAttrService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author dyqstart
 * @create 2021-08-05-16:54
 * @since 1.0.0
 */
@RestController
@RequestMapping("/admin/product")
@Slf4j
public class SpuController {

    @Autowired
    SpuInfoService spuInfoService;

    @Autowired
    SpuImageService spuImageService;

    @Autowired
    SpuSaleAttrService spuSaleAttrService;


    /**
     * http://192.168.200.1:88/admin/product/1/10?category3Id=2
     * 获取spu分页列表
     * @param category3Id
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/{page}/{limit}")
    public Result<Page<SpuInfo>> spuInfoPageList(@RequestParam("category3Id") Long category3Id,
                                                 @PathVariable("page") Long page,
                                                 @PathVariable("limit") Long limit) {
        Page<SpuInfo> spuInfoPageSetting = new Page<>(page,limit);
        QueryWrapper<SpuInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("category3_id",category3Id);

        Page<SpuInfo> infoPage = spuInfoService.page(spuInfoPageSetting, wrapper);

        return Result.ok(infoPage);
    }

    /**
     * select si.*,sm.img_name,sm.img_url,ssa.base_sale_attr_id,ssa.sale_attr_name,ssav.sale_attr_value_name
     * from spu_info si
     * left join spu_image sm
     *     on sm.spu_id = si.id
     * left join spu_sale_attr ssa
     *     on ssa.spu_id = si.id
     * left join spu_sale_attr_value ssav
     *     on ssav.base_sale_attr_id = ssa.base_sale_attr_id
     * where si.id = 27
     *
     */

    /**
     *  sku_info  spu_image  spu_sale_attr  spu_sale_attr_value
     * @return
     */
    @PostMapping("saveSpuInfo")
    public Result saveSpuInfo(@RequestBody SpuInfo spuInfo){
        spuInfoService.bigSaveInfo(spuInfo);
        return Result.ok();
    }


    /**
     *   {
    *             "id":33,
    *             "spuId":4,
    *             "imgName":"list.jpg",
    *             "imgUrl":"http://47.93.118.241/group1/M00/00/00/L1128V47i9GARYUWAADxtUrmGBA961.jpg"
     *         }
     * @param spuId
     * @return
     */
    @GetMapping("spuImageList/{spuId}")
    public Result<List<SpuImage>> spuImageListBySpuId(@PathVariable("spuId") Long spuId){

        QueryWrapper<SpuImage> wrapper = new QueryWrapper<>();
        wrapper.eq("spu_id",spuId);

        List<SpuImage> list = spuImageService.list(wrapper);
        return Result.ok(list);
    }


    /**
     * [
     *         {
     *             "id":8,
     *             "spuId":4,
     *             "baseSaleAttrId":1,
     *             "saleAttrName":"选择颜色",
     *             "spuSaleAttrValueList":[
     *                 {
     *                     "id":18,
     *                     "spuId":4,
     *                     "baseSaleAttrId":1,
     *                     "saleAttrValueName":"红色",
     *                     "saleAttrName":"选择颜色",
     *                     "isChecked":null
     *                 },
     *             ]
     *         }
     */

    @GetMapping("spuSaleAttrList/{spuId}")
    public Result<List<SpuSaleAttr>> spuSaleAttrListBySpuId(@PathVariable("spuId") Long spuId){

        List<SpuSaleAttr> list = spuSaleAttrService.getSpuAttrAndValue(spuId);

        return Result.ok(list);

    }



}
